package com.ly.airline.pojo;

import cn.hutool.core.bean.BeanUtil;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：xxl
 * @ClassName Car
 * @date ：Created in 2022/4/29 15:11
 * @description： TODO
 * @modified By：
 * @version: 1.0
 */
@Data
public class Cart {

    private Map<String,Orderitem> items;

    private Integer totalPrice;

    public Cart() {
        this.items = new HashMap<>();
        this.totalPrice = 0;
    }

//    public void setTotalPrice() {
//        this.totalPrice = 0;
//        for (Orderitem value : items.values()) {
//            this.totalPrice=this.totalPrice+value.getShuliang()*value.getDanjia();
//        }
//    }

    public Integer getTotalPrice() {
        totalPrice = 0;
        for (Orderitem value : items.values()) {
            totalPrice=totalPrice+value.getShuliang()*value.getDanjia();
        }
        return totalPrice;
    }

    public void addItem(String key,Orderitem orderitem){
        Orderitem orderitem1 = items.get(key);
        if (BeanUtil.isNotEmpty(orderitem1)){
            orderitem.setShuliang(orderitem.getShuliang()+orderitem1.getShuliang());
        }
        items.put(key,orderitem);
    }

    public void deleteItem(String key){
        items.remove(key);
    }
}
